package com.zzyl.framework.interceptor;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.UserThreadLocal;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @SneakyThrows
    public boolean isExclude() {
        try {
            // 安全地获取当前请求
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            String requestURI = request.getRequestURI();
            if(requestURI.startsWith("/member")) {
                return true;
            }
            return false;
        } catch (IllegalStateException e) {
            // 如果不在Web请求上下文中，返回默认值
            return true;
        }
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        if(!isExclude()) {
            this.strictInsertFill(metaObject, "createBy", String.class, loadUserId() + "");
        }else {
            this.strictInsertFill(metaObject, "createBy", String.class, loadMiniUserId() +"");
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        if(!isExclude()) {
            this.strictUpdateFill(metaObject, "updateBy", String.class, loadUserId() + "");
        }else {
            this.strictUpdateFill(metaObject, "updateBy", String.class, loadMiniUserId() + "");
        }

    }

    /**
     * 获取当前登录人的ID
     *
     * @return
     */
    private static Long loadUserId() {

        // 获取当前登录人的id
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (ObjectUtils.isNotEmpty(loginUser)) {
                return loginUser.getUserId();
            }
            return 1L;
        } catch (Exception e) {
            return 1L;
        }
    }
    /**
     * 获取当前小程序登录人的ID
     */
    private static Long loadMiniUserId() {
        // 获取当前登录人的id
        try {
            Long loginUser = UserThreadLocal.getUserId();
            if (ObjectUtils.isNotEmpty(loginUser)) {
                return loginUser;
            }
            return 1L;
        } catch (Exception e) {
            return 1L;
        }
    }
}